<!-- eslint-disable vue/no-duplicate-attributes -->
<!-- eslint-disable vue/max-attributes-per-line -->
<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button type="primary" size="small" @click="hAdd(1, '0')"
            >添加权限</el-button
          >
        </div>
        <el-table border :data="list" row-key="id">
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <!-- <template> -->
            <template #default="{ row }">
              <el-button
                v-if="row.type == 1"
                type="text"
                @click="hAdd(2, row.id)"
                >添加</el-button
              >
              <!-- <el-button type="text" @click="hAdd">添加</el-button> -->
              <el-button type="text" @click="hEdit(row.id)">编辑</el-button>
              <el-button type="text" @click="hDel(row.id)">删除</el-button>
              <!-- @click.native="hDel(data.id)" -->
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </div>
    <el-dialog :visible.sync="showDialog" title="弹层标题" @close="close">
      <!-- 表单内容 -->
      <el-form label-width="100px">
        <el-form-item label="权限名称">
          <el-input v-model="formData.name" />
        </el-form-item>
        <el-form-item label="权限标识">
          <el-input v-model="formData.code" />
        </el-form-item>
        <el-form-item label="权限描述">
          <el-input v-model="formData.description" />
        </el-form-item>
        <el-form-item label="权限启用">
          <el-switch
            v-model="formData.enVisible"
            active-text="启用"
            active-value="1"
            inactive-text="不启用"
            inactive-value="0"
          />
        </el-form-item>
      </el-form>

      <template #footer>
        <div style="text-align: right">
          <el-button @click="showDialog = false">取消</el-button>
          <el-button type="primary" @click="hSubmit">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
// import { tranListToTreeData } from '@/utils'
import { tranListToTreeData2 } from '@/utils'
import {
  getPermissionList,
  addPermission,
  updatePermission,
  delPermission,
  getPermissionDetail
} from '@/api/permission'

export default {
  name: 'Permission',
  data() {
    return {
      list: [],
      showDialog: false, // 是否显示弹层
      formData: {
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        enVisible: '0', // 开启
        pid: '', // 添加到哪个节点下
        type: '' // 类型
      },
      isEdit: false
    }
  },
  created() {
    this.loadPermissionList()
  },
  methods: {
    async loadPermissionList() {
      const res = await getPermissionList()
      // console.log('获取权限数据', res)
      this.list = tranListToTreeData2(res.data)
      console.log(this.list, 'this.list')
    },
    hAdd(type, id) {
      this.isEdit = false
      this.showDialog = true
      // 记录当前添加的关键信息
      this.formData.type = type
      this.formData.pid = id
    },
    // 点击确认
    async hSubmit() {
      this.isEdit ? this.doEdit() : this.doAdd()
      console.log(this.formData)
      const res = await addPermission(this.formData)
      console.log(res)
      // 1. 强制重置表单内容
      // this.formData = {
      //   name: '', // 名称
      //   code: '', // 权限标识
      //   description: '', // 描述
      //   enVisible: '0', // 开启
      //   pid: '', // 添加到哪个节点下
      //   type: '' // 类型
      // }
      this.showDialog = false
      this.loadPermissionList()
    },
    async doAdd() {
      await addPermission(this.formData)
    },
    hDel(id) {
      this.$confirm('此操作将永久删除部门, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          // 做删除
          this.doDel(id)
        })
        .catch(() => {})
    },
    async doDel(id) {
      try {
        const res = await delPermission(id)
        console.log(res)
        // 1. 提示
        this.$message({ type: 'success', message: '删除成功!' })
        // 2. 更新数据
        this.loadPermissionList()
      } catch (err) {
        console.log(err)
        this.$message({ type: 'error', message: '删除失败' })
      }
    },
    // 用户点击了编辑
    async hEdit(id) {
      /*
      1-显示对话框
      2-判断是编辑还是添加
      3-根据id获取当前数据并回显到表单
      4-发送编辑的请求
      5-关闭对话框+刷新页面+清空表单数据
      */
      this.showDialog = true
      this.isEdit = true // 设置是编辑
      /*  3-根据id获取当前数据并回显到表单 */
      const res = await getPermissionDetail(id)
      console.log(res, 666)
      this.formData = res.data
    },
    /* 编辑发送请求 */
    async doEdit() {
      // 4 - 发送编辑的请求
      await updatePermission(this.formData)
    },
    close() {
      this.formData = {
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        enVisible: '0', // 开启
        pid: '', // 添加到哪个节点下
        type: '' // 类型
      }
    }
  }
}
</script>
